<?php

namespace App\Http\Controllers\API;

use App\Model\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ShopController extends Controller
{

    /**
     * @abstract get all shop data
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        $arrRequestParams = $request->all();
        if(array_key_exists('shopname',$arrRequestParams) && $arrRequestParams['shopname'] != ''){
            $arrShop = DB::table('shops')->where('shopname', 'like', '%'.$arrRequestParams['shopname'].'%')->get();
        }else{
            $arrShop = DB::table('shops')->get();
        }
        return response()->json(['code' => 0, 'message' => 'success', 'data' => $arrShop],200);
    }

    /**
     * @abstract add new shop
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request){
        $arrInsertData = $request->all();
        $arrInsertData['longtitude'] = '123';
        $arrInsertData['latitude'] = '123';
        $objShop = new Shop();
        $objShop->shopname = $arrInsertData['shopname'];
        $objShop->address = $arrInsertData['address'];
        $objShop->longtitude = $arrInsertData['longtitude'];
        $objShop->latitude = $arrInsertData['latitude'];
        $objShop->telephone = $arrInsertData['telephone'];
        $objShop->showpath = $arrInsertData['showpath'];
        $objShop->status = 1;
        $objShop->save();
        return response()->json(['code' => 0, 'message' => 'insert success'],200);
    }

    /**
     * @abstract update shop data
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request,$id){
        $arrUpdateInfo = $request->all();
        $objShop = Shop::findOrFail($id);
        $objShop->shopname = $arrUpdateInfo['shopname'];
        $objShop->address = $arrUpdateInfo['address'];
        $objShop->longtitude = $arrUpdateInfo['longtitude'];
        $objShop->latitude = $arrUpdateInfo['latitude'];
        $objShop->telephone = $arrUpdateInfo['telephone'];
        $objShop->showpath = $arrUpdateInfo['showpath'];
        $objShop->save();
        return response()->json(['code' => 0, 'message' => 'update success'],200);
    }

    /**
     * @abstract select shop data by id
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id){
        $objShop = Shop::find($id);
        return response()->json(['code' => 0, 'message' => 'success', 'data' => $objShop],200);
    }


    /**
     * @abstract delete shop data by id
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id){
        $deleteCount = Shop::destroy($id);
        if($deleteCount){
            return response()->json(['code' => 0, 'message' => 'delete success'],200);
        }else{
            return response()->json(['code' => 1, 'message' => 'delete failed'],200);
        }
    }
}
